package com.sscaipiao.android.activity;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.BaseWoDeUIActivity;
import com.sscaipiao.android.adapter.WoDeItemAdapt;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.Lottery;
import com.sscaipiao.android.model.MyLottery;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.DateUtil;
import com.sscaipiao.android.utils.StringUtil;

import java.util.*;

/**
 * Created by ppona_000 on 14-1-22.
 */
public class WoDeActivity extends BaseWoDeUIActivity implements AdapterView.OnItemClickListener {
	private ListView mListView;
	private WoDeItemAdapt mAdapter ;
	private List<MyLottery> mList = new ArrayList<MyLottery>();
	private PopupWindow mPopupWindow;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.wo_de_activity);
		setTitleText("我的彩票");
		mListView = (ListView) findViewById(R.id.listView);
		mAdapter = new WoDeItemAdapt(this, mList);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		showProgressDialog();
		Thread thr = new Thread(mTask, "1");
		thr.start();

	}

	Runnable mTask = new Runnable() {
		public void run() {
			setupWoDeList();
			closeProgressDailog();
		}
	};
	private void setupWoDeList() {
		List<String> bets = CaipiaoServiceHelper.getBetList(this, user,
				null,
				DateUtil.getDate(null, new Date((new Date()).getTime()- 1000*60*60*24*2)) ,
				DateUtil.getDate(null, new Date()),
				null, null, Constants.BET_STATUS_BINGO);

		mList.clear();

		for (int j = 0; j < bets.size(); j++) {
			MyLottery ml = new MyLottery();
			String value = bets.get(j);
			String item[] = value.split(Constants.STRING_SPLITE_COMPLEX);
			ml.setBetTime(item[0]);
			ml.setLotteryCode(item[1]);
			ml.setLotteryName(item[2]);
			ml.setPeriodNo(item[3]);
			ml.setLotteryNum(item[4]);
			ml.setBetMoney(item[5]);
			ml.setAwardInfo(item[6]);
			ml.setAwardMoney(item[7]);
			mList.add(ml);
		}
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
			}
		});

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		int id = v.getId();
		if (id == R.id.title_del) {

		}
		if (id == R.id.title_reflash) {

		}
		if (id == R.id.title_arrow) {
			showPopUpwindow();
		}
		if (id == R.id.btn_quanbudingdan || id == R.id.btn_wodezhuihao
				|| id == R.id.btn_faqihemai || id == R.id.btn_canyuhemai
				|| id == R.id.btn_zhongjiangdingdan) {
			if (mPopupWindow != null) {
				mPopupWindow.dismiss();
			}
		}

	}

	private void showPopUpwindow() {
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
		}

		LayoutInflater mLayoutInflater = LayoutInflater.from(this);
		View contentView = mLayoutInflater.inflate(R.layout.wo_de_popup_window, null);
		Button button = null;
		button = (Button) contentView.findViewById(R.id.btn_quanbudingdan);
		button.setOnClickListener(this);
		button = (Button) contentView.findViewById(R.id.btn_wodezhuihao);
		button.setOnClickListener(this);
		button = (Button) contentView.findViewById(R.id.btn_faqihemai);
		button.setOnClickListener(this);
		button = (Button) contentView.findViewById(R.id.btn_canyuhemai);
		button.setOnClickListener(this);
		button = (Button) contentView.findViewById(R.id.btn_zhongjiangdingdan);
		button.setOnClickListener(this);
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		int xpos = mTitleText.getWidth()/2-outMetrics.widthPixels/4;

		mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.showAsDropDown(mTitleText, xpos, 0);

	}
}